const { HTTP } = require('../../utils/http');
const { api } = require('../../utils/api');
const { request } = new HTTP();
const app = getApp();

Page({
  data: {
    active: 0,
    tabs: [],
  },
  
  onLoad() {
    this._queryCategory();
  },
  onClick(event) {
    console.log('🚀 ~ file: index.js ~ line 16 ~ onClick ~ event', event)
    this.setData({
      active: event.detail.index
    })

  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1
    });
  },

  selectPhoto() {
    const categoryId = this.data.tabs[this.data.active].id;
    const categoryTitle = this.data.tabs[this.data.active].title;

    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        app.globalData.selectPhoto = tempFilePaths[0];
        wx.navigateTo({url: `../publish/index?categoryId=${categoryId}&categoryTitle=${categoryTitle}`});
      }
    })
  },

  // 发布文字
  selectText() {
    const categoryId = this.data.tabs[this.data.active].id;
    console.log('🚀 ~ file: index.js ~ line 35 ~ selectText ~ this.data.active', this.data.active)
    const categoryTitle = this.data.tabs[this.data.active].title;
    console.log('🚀 ~ file: index.js ~ line 36 ~ selectText ~ categoryTitle', categoryTitle)
    wx.navigateTo({url: `../publish/index?categoryId=${categoryId}&categoryTitle=${categoryTitle}`});
  },

  // 获取话题分类
  async _queryCategory() {
    const {returnCode = '', beans = []} = await request({
      url: api.queryCategory,
      method: 'get',
      data: {}
    });
    if (returnCode === '0000') {
      this.setData({
        tabs: beans
      });
    }
  }
})
